package com.infomanage.util;

import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.infomanage.dao.DeptDAO;
import com.infomanage.dao.PersonDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.exception.DaoException;
import com.infomanage.po.Department;
import com.infomanage.po.Person;
import com.infomanage.po.User;
import com.jiuqi.webtree.TreeNode;
import com.jiuqi.webtree.TreeNodeList;
import com.jiuqi.webtree.WebTreeInfo;
import com.jiuqi.webtree.WebTreeIntf;

/**
 * 员工树
 *
 */
public class PersonTree implements WebTreeIntf{
	private DeptDAO deptDAO = null;
	private PersonDAO personDAO = null;
	private int deptId = -1;
	private int roleId = -1;
	private boolean isMultiSeletTree = false;
	private String existPersonIds = "";
	public String getExistPersonIds() {
		return existPersonIds;
	}

	public void setExistPersonIds(String existPersonIds) {
		this.existPersonIds = existPersonIds;
	}

	public PersonTree(HttpServletRequest request, boolean isMultiSeletTree){
		deptDAO = new DeptDAO();
		personDAO = new PersonDAO();
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			deptId = user.getAssociatedDept();
			roleId = user.getRoleId();
		}
		this.isMultiSeletTree = isMultiSeletTree;
	}

	public boolean delNode(String arg0, boolean arg1, String arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	public String getErrorMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	public String[] getNodePaths(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public TreeNodeList getNodes(String parentId, int arg1, WebTreeInfo arg2) {
		TreeNodeList childNodes = new TreeNodeList(1, 1);
		try {
			if(parentId == null || parentId.equals("")){
				parentId = "-1";
			}
			List<Department> list = deptDAO.findChildren(Integer.parseInt(parentId));
			if(list != null){
				for(Department dept : list){
					if(dept.getLevel() == 2){
						if(roleId == RoleType.ZDADMIN.value()){
							if(deptId == dept.getDeptId()){
								TreeNode treeNode = new TreeNode(String.valueOf(dept.getDeptId()), dept.getName(), true, null, false);
								childNodes.addNode(treeNode);
							}
						}else{
							TreeNode treeNode = new TreeNode(String.valueOf(dept.getDeptId()), dept.getName(), true, null, false);
							childNodes.addNode(treeNode);
						}
					}else{
						TreeNode treeNode = new TreeNode(String.valueOf(dept.getDeptId()), dept.getName(), true, null, false);
						childNodes.addNode(treeNode);
					}
				}
			}
			Department dept = deptDAO.find(Department.class, Integer.parseInt(parentId));
			boolean hasAuthority = false;
			if(dept == null){//大队
				if(roleId == RoleType.SYSADMIN.value() || roleId == RoleType.DDADMIN.value()){
					hasAuthority = true;
				}
			}else{
				if(roleId == RoleType.SYSADMIN.value() || roleId == RoleType.DDADMIN.value()){
					hasAuthority = true;
				}else if(deptDAO.isChildOf(dept.getDeptId(), deptId)){
					hasAuthority = true;
				}
			}
			List<Person> personList = personDAO.findByDept(Integer.parseInt(parentId));
			if(personList != null){
				for(Person person : personList){
					if(!hasAuthority)
						break;
					TreeNode treeNode = new TreeNode(String.valueOf(person.getPersonId()), person.getName(), false, null, isMultiSeletTree ? true : false);
					if(isMultiSeletTree && existPersonIds != null && existPersonIds.contains(String.valueOf(person.getPersonId()))){
						treeNode.setChecked(1);
					}
					childNodes.addNode(treeNode);
				}
				
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return childNodes;
	}

	public boolean insertNode(String arg0, String arg1, Hashtable arg2) {
		// TODO Auto-generated method stub
		return false;
	}

}
